package org.neurone.action.userspace;

import java.util.List;

import org.neurone.action.BaseActionSupport;
import org.neurone.context.MessageConst;
import org.neurone.domain.UserInfo;
import org.neurone.form.UserInfoForm;
import org.neurone.userspace.userinfo.UserFace;
import org.neurone.userspace.userinfo.UserMgr;
import org.neurone.userspace.userinfo.impl.UserFaceLocal;
import org.neurone.userspace.userinfo.impl.UserFaceLocalFactory;

public class UserInfoListAction extends BaseActionSupport {
	private List<UserInfo> userList;
	private UserInfoForm user;
	private UserMgr userMgr;
	private UserFaceLocalFactory userFaceFactory;
	private Long userid;
	private boolean modType;
	
	public List<UserInfo> getUserList() {
		return userList;
	}
	public void setUserList(List<UserInfo> userList) {
		this.userList = userList;
	}
	public UserMgr getUserMgr() {
		return userMgr;
	}
	public Long getUserid() {
		return userid;
	}
	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}
	public void setUserid(Long userid) {
		this.userid = userid;
	}
	public UserInfoForm getUser() {
		return user;
	}
	public void setUser(UserInfoForm user) {
		this.user = user;
	}	
	public UserFaceLocalFactory getUserFaceFactory() {
		return userFaceFactory;
	}
	public void setUserFaceFactory(UserFaceLocalFactory userFaceFactory) {
		this.userFaceFactory = userFaceFactory;
	}
	
	
	public boolean isModType() {
		return modType;
	}
	public void setModType(boolean modType) {
		this.modType = modType;
	}
	private String showList()
	{
		this.userList = userMgr.findAll(0, 0);
		return success();
	}
	
	
	public String defaultShow()
	{
		this.modType = false;
		return showList();
	}

	public String prepareMod()
	{
		this.modType = true;
		if(userid != null)
		{
			if(this.user == null)
			{
				UserFaceLocal u = this.userFaceFactory.getNewUserFace(userid);
				if(u.isPersistentAvailable())
				{

					this.user = new UserInfoForm();

					this.user.setEmail(u.getEmail());
					this.user.setImage(u.getImage());
					this.user.setPasswd(u.getPasswd());
					this.user.setRealName(u.getRealName());
					this.user.setStatus(u.getStatus());
					this.user.setUserName(u.getUserName());
					this.user.setUserNick(u.getUserNick());

				}else
				{
					this.addActionError(MessageConst.NO_SUCH_USER);
					return ERROR;
				}
			}
		}
		else
		{
			this.user = null;
		}
		
		return showList();		
	}
	
	private String newUser()
	{
		if(user !=null)
		{
			this.userid = userMgr.newUser(this.user);
			return success();
		}
		else
		{
			return INPUT;
		}
	}
	private String updateUser()
	{
		if(user !=null)
		{
			UserFaceLocal u = userFaceFactory.getNewUserFace(this.userid);
			
			u.setEmail(this.user.getEmail());
			u.setImage(this.user.getImage());
			u.setRealName(this.user.getRealName());
			u.setStatus(this.user.getStatus());
			u.setUserName(this.user.getUserName());
			u.setUserNick(this.user.getUserNick());
			
			if(! this.user.getPasswd().equals(u.getPasswd()))
			{
				u.setPasswd(this.user.getPasswd());
			}
			
			u.flush();
			
			return success();
		}
		else
		{
			return INPUT;
		}
	}
	
	public String modUser()
	{
		if(userid !=null)
		{
			return updateUser();
		}
		else
		{
			return newUser();
		}
	}	
	public String deleteUser()
	{
		if(userid != null)
		{
			userMgr.delete(userid);
		}
		return showList();
	}		
}
